/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 31, 2009
 * Time: 10:46:21 AM
 * To change this template use File | Settings | File Templates.
 */
import java.util.Iterator;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Element;

// Referenced classes of package dk.brics.dsd:
//            Field, InternalSchemaErrorException, Context, FieldEvaluationException,
//            Schema, Boolexp, Validator

class AttributeField extends Field
{

    AttributeField(Element element, Schema schema)
    {
        name = element.getAttributeValue("name");
        if(name == null)
        {
            throw new InternalSchemaErrorException();
        } else
        {
            name_local = Schema.pnameLocal(name);
            name_uri = Schema.panameURI(name, element);
            exp = Boolexp.parseOneOptional(element, schema);
            String s = element.getAttributeValue("type");
            qname = s != null && s.equals("qname");
            qaname = s != null && s.equals("qaname");
            return;
        }
    }

    public String evaluate(Context context, Element element)
        throws FieldEvaluationException
    {
        error = false;
        Element element1 = findSelected(new Context(element, context.e, context.v), exp);
        if(element1 == null)
            throw new FieldEvaluationException("not exactly one element selected at attribute field '" + name + "'");
        String s = null;
        Iterator iterator = element1.getAttributes().iterator();
        do
        {
            if(!iterator.hasNext())
                break;
            Attribute attribute = (Attribute)iterator.next();
            if(attribute.getNamespaceURI().equals("http://www.brics.dk/DSD/2.0/meta"))
                continue;
            String s1 = attribute.getName();
            String s2;
            if(attribute.getNamespacePrefix().equals(""))
                s2 = null;
            else
                s2 = attribute.getNamespaceURI();
            if(!Schema.matchPAname(name_local, name_uri, s1, s2))
                continue;
            s = attribute.getValue();
            break;
        } while(true);
        if(s == null)
            throw new FieldEvaluationException("selected element has no '" + name + "' attribute");
        s = Validator.whitespaceString(true, s);
        if(qname || qaname)
        {
            s = fixQname(s, element1, qaname);
            if(s == null)
                throw new FieldEvaluationException(qname ? "invalid qname" : "invalid qaname");
        }
        return s;
    }

    public Element toXML(Context context)
    {
        Element element = new Element("attributefield", "http://www.brics.dk/DSD/2.0");
        if(exp != null)
            element.addContent(exp.toXML(context));
        element.setAttribute("name", name);
        if(qname)
            element.setAttribute("type", "qname");
        else
        if(qaname)
            element.setAttribute("type", "qaname");
        return element;
    }

    String name;
    String name_local;
    String name_uri;
    Boolexp exp;
    boolean qname;
    boolean qaname;
}

